import datetime
from other.cfg import Config
from other.change_format import change_format
from other.sql_montage import sqlMontage
from other.status import Status
from server.Mysql import MySQL

class design:
  def __init__(self):
     self.cfg = Config()
     self.st = Status()
     self.db = MySQL(self.cfg.res['mysql']['database'], 'design')
     self.cf = change_format()
     self.sm = sqlMontage()
     
  def get_design_list(self, data):
      result = {'msg': '成功', 'code': 200, 'data': [], 'total': None}
      sqls = "where 1=1 "
      sql = ""
      pages = ''
      if data != {}:
          if data.get('pageNum') is not None and data.get('pageSize') is not None:
              if data['pageNum'] != '' and data['pageSize'] != '':
                  pages = " limit {},{}".format(int(data['pageNum']) * int(data['pageSize']) - int(data['pageSize']),
                                                int(data['pageSize']))
      total = self.db.get(sqls + sql)
      result['total'] = len(total)
      if sql != "":
          sql = sqls + sql + ' ORDER BY `order_num` ASC ' + f'{pages}'
      else:
          sql = sqls + ' ORDER BY `order_num` ASC ' + f'{pages}'
      res = self.db.get(sql)
   
      if res != ():
          for r in res:
              for key, value in r.items():
                  if isinstance(value, datetime.datetime):
                      r[key] = value.strftime("%Y-%m-%d %H:%M:%S")
              result['data'].append(r)
      result = self.cf.changes_format(result)
      return result
    
    
  # 新增设计
  def design_add(self, data, userInfo):
      result = {'msg': '成功', 'code': 200}
      print("aaa",data)
      times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      data['create_by'] = userInfo['userName']
      data['create_time'] = times
      add_sql = self.sm.add_sql_data(data, 'design')
      print('add_sql',add_sql)
      self.db.adds(add_sql, 'design')
      return result
    
    
  # 修改设计
  def design_edit(self, data, userInfo):
      result = {'msg': '成功', 'code': 200}
      times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      if 'createTime' in data:
          del data['createTime']
      data['update_by'] = userInfo['userName']
      data['update_time'] = times
      
      edit_sql = self.sm.edit_sql_data(data, 'design', 'design_id', data['designId'])
      self.db.edits(edit_sql, 'design')
      return result

    
  # 查看设计
  def get_design_by_id(self, designId):
      result = {'msg': '成功', 'code': 200, 'data': {}}
      sql = f"where `design_id`='{designId}'"
      res = self.db.get(sql)
      result['data'] = res[0]
      result = self.cf.changes_format(result)
      return result

  # 查询标题
  def get_design_title(self, designId):
      result = {'msg': '成功', 'code': 200, 'data': {}}
      sql = f"where `design_id`='{designId}'"
      res = self.db.get(sql)
      result['data'] = {'value': res[0]['title']}
      print('result------',result)
      result = self.cf.changes_format(result)
      return result
  
  # 删除设计
  def design_delete(self, data):
      result = {'msg': '成功', 'code': 200}
      times = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      ids = [int(x) for x in data.split(',')]
      for i in ids:
          sql = "where design_id='{}'".format(i)
          self.db.delete(sql)
      return result


  